package com.lin.controller;

import com.lin.pojo.User;
import com.lin.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName UserController
 * @Description 用户界面控制
 * @Author LinZePeng
 * @Date 2021/5/3 20:14
 * @Version 1.0
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * @Description 接受前端返回的JSON对像，然后对信息进行修饰
	 * @Param [user]
	 * @return void
	 */
	@RequestMapping("/add/user")
	@ResponseBody
	public void addUser(String user){
		userService.toAddUser(user);
	}


	/**
	 * @Description 检查注册姓名
	 * @Param [name]
	 * @return java.lang.Boolean
	 */
	@RequestMapping("/check/{name}")
	@ResponseBody
	public Boolean checkName(@PathVariable String name){
		User user = userService.queryByName(name);
		return user == null;
	}

	/**
	 * @Description 登录信息的检查
	 * @Param [name, phone, email]
	 * @return java.lang.Boolean
	 */
	@RequestMapping("/check/login")
	@ResponseBody
	public Boolean check(String name, String phone, String email, HttpServletResponse httpResponse){
		if (userService.queryCheck(name,phone,email))
		{
			Cookie cookie = new Cookie("userID",userService.queryByName(name).getUserID());
			cookie.setPath("/");
			httpResponse.addCookie(cookie);
			return true;
		}
		else {
			return false;
		}
	}


}
